package com.dayu.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dayu.lease.model.entity.*;
import com.dayu.lease.web.admin.mapper.*;
import com.dayu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.dayu.lease.web.admin.vo.agreement.AgreementVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Resource
    private LeaseAgreementMapper leaseAgreementMapper;
    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Resource
    private PaymentTypeMapper paymentTypeMapper;
    @Resource
    private LeaseTermMapper leaseTermMapper;

    @Override
    public IPage<AgreementVo> pageAgreement(Page<AgreementVo> page, AgreementQueryVo queryVo) {
        return leaseAgreementMapper.pageAgreement(page,queryVo);
    }

    @Override
    public AgreementVo getAgreementById(Long id) {
        AgreementVo vo = new AgreementVo();
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        LambdaQueryWrapper<ApartmentInfo> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(ApartmentInfo::getId, leaseAgreement.getApartmentId());
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectOne(wrapper1);
        LambdaQueryWrapper<RoomInfo> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(RoomInfo::getId, leaseAgreement.getRoomId());
        RoomInfo roomInfo = roomInfoMapper.selectOne(wrapper2);
        LambdaQueryWrapper<PaymentType> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(PaymentType::getId, leaseAgreement.getPaymentTypeId());
        PaymentType paymentType = paymentTypeMapper.selectOne(wrapper3);
        LambdaQueryWrapper<LeaseTerm> wrapper4 = new LambdaQueryWrapper<>();
        wrapper4.eq(LeaseTerm::getId, leaseAgreement.getLeaseTermId());
        LeaseTerm leaseTerm = leaseTermMapper.selectOne(wrapper4);
        BeanUtils.copyProperties(leaseAgreement, vo);
        vo.setLeaseTerm(leaseTerm);
        vo.setApartmentInfo(apartmentInfo);
        vo.setRoomInfo(roomInfo);
        vo.setPaymentType(paymentType);
        return vo;
    }
}




